package com.example.application8_2;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.room.Insert;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Fragment2#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Fragment2 extends Fragment{

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    String TAG = "TAG";
    DbHelper dbHelper;
    private SQLiteDatabase db;
    private EditText Met_textView2;
    private EditText Met_textView3;
    private TextView mTvShow;
    private Button Mbt_button2;
    private Button Mbt_button;

    public Fragment2() {
        // Required empty public constructor
        super(R.layout.fragment_2);

    }


    @Override
    public void onViewCreated(@NonNull View view, @NonNull Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        Met_textView2 = (EditText)getActivity().findViewById(R.id.editTextTextMultiLine);
        Met_textView3 = (EditText)getActivity().findViewById(R.id.editTextTextMultiLine2);
//        Met_textView4 = (EditText)getActivity().findViewById(R.id.textView4);
        mTvShow = (TextView)getActivity().findViewById(R.id.textView4);
        dbHelper = new DbHelper(getActivity(),"users",null,1);


        Mbt_button2 = (Button)getActivity().findViewById(R.id.button2);
        Mbt_button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                db = dbHelper.getWritableDatabase();
                Insert();

                Met_textView2.setText("");
                Met_textView3.setText("");

                //显示数据
                Cursor cursor = db.query("users",null,null,null,null,null,null);
                if (cursor.getCount() == 0){
                    mTvShow.setText("");
                } else {
                    cursor.moveToFirst();
                    mTvShow.setText("\n" + "id:" + cursor.getString(0) + "   username:"+cursor.getString(1) + "   age:" + cursor.getString(2));
                }
                while (cursor.moveToNext()){
                    mTvShow.append("\n" + "id:" + cursor.getString(0) + "   username:"+cursor.getString(1) + "   age:" + cursor.getString(2));
                }
                cursor.close();
            }
        });

        Mbt_button = (Button)getActivity().findViewById(R.id.button);
        Mbt_button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                //得到数据库
                db = dbHelper.getWritableDatabase();
                //条件查询数据
                Cursor cursor = db.query("users", null, "age < 18", null, null, null, null);
                if (cursor.getCount() == 0) {
                    mTvShow.setText("");
                } else {
                    cursor.moveToFirst();
                    mTvShow.setText("\n" + "id:" + cursor.getString(0) + "   username:" + cursor.getString(1) + "   age:" + cursor.getString(2));
                }
                while (cursor.moveToNext()) {
                    mTvShow.append("\n" + "id:" + cursor.getString(0) + "   username:" + cursor.getString(1) + "   age:" + cursor.getString(2));
                }

                cursor.close();
//                Query();  "/n" + "id:" + cursor.getInt(1) + " username:" + cursor.getString(2) + " age:" + cursor.getString(3)
            }
        });

    }

//    public void Query(){
//        Cursor cursor= db.query("users",null,null,null,null,null,null);
//        if(cursor.moveToFirst()){
//            do{
//                int id = cursor.getInt(cursor.getColumnIndex("_id"));
//                String title = cursor.getString(cursor.getColumnIndex("username"));
//                String content = cursor.getString(cursor.getColumnIndex("age"));
//                Log.i(TAG,"_id:" + id);
//                Log.i(TAG, "username:" + title);
//                Log.i(TAG, "age:" + content);
//            }while(cursor.moveToNext());
//        }
//        cursor.close();
//    }


    public void Insert(){
        ContentValues values = new ContentValues();
        values.put("username", Met_textView2.getText().toString());
        values.put("age", Met_textView3.getText().toString());
        db.insert("users", null, values);
    }






    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Fragment2.
     */
    // TODO: Rename and change types and number of parameters
    public static Fragment2 newInstance(String param1, String param2) {
        Fragment2 fragment = new Fragment2();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_2, container, false);
    }




}